﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fyp.Models;
using WebMatrix.WebData;
using System.Web.Security;

namespace Fyp.Controllers
{
    public class SubmitArtifactController : Controller
    {
        //
        // GET: /SubmitArtifact/

        FYPEntities5 db = new FYPEntities5();
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public string ReplyComment(FormCollection c)
        {
            //change

            string comi = c["RepComment"];
            global.comment_t = comi;

            var myquu = from artii in db.SubmitArtifacts
                        where global.Gid_T == artii.GId
                        select artii;

            foreach (var stp2 in myquu)
            {
                stp2.Comment_T = global.comment_t;
            }

            db.SaveChanges();
            return "submitted";
        }
        [HttpPost]
        public ActionResult TeacherView(FormCollection c)
        {
            int getcateId = int.Parse(c["GroupField"]);
            global.Gid_T = getcateId;

            var q1 = from s in db.Students
                     from p in db.Projects
                     from g in db.Groups
                     from art in db.SubmitArtifacts
                     where getcateId == art.GId && art.GId == g.GId && g.PId == p.PId
                     select new
                     {
                         p.Title,
                         g.GId,
                         p.AdvisorName,
                         art.Comment_S,
                         art.ArtifactTitle,
                         art.FilePath
                     };
            foreach (var stp in q1)
            {
                ViewBag.PTitle = stp.Title;
                ViewBag.GId = stp.GId;
                ViewBag.Advisor = stp.AdvisorName;
                ViewBag.Arti_Title = stp.ArtifactTitle;
                ViewBag.Comi_S = stp.Comment_S;
                ViewBag.path = stp.FilePath;
            }
            // global.Gid_T = getcateId;
            ViewBag.id = getcateId;

            return View();
        }
        public ActionResult TeacherView()
        {
            var q2 = from g1 in db.Groups
                     select g1;
            foreach (var gquery in q2)
            {
                Group gg = new Group();
                gg.GId = gquery.GId;
                global.glist.Add(gg);
            }

            //change
            var myquu = from artii in db.SubmitArtifacts
                        where global.Gid_T == artii.GId
                        select artii;
            ViewData["data"] = myquu.ToList();
            return View();
        }


        /*
                [HttpPost]
                public ActionResult StudentView(FormCollection c)
                {
           
                     int getcateId = int.Parse(c["GroupFields"]);
                     global.Gid = getcateId;
            
                    // ar.GId = getcateId;
                    // db.Artifacts.Add(ar);
                     //db.SaveChanges();
            
                     var q1 = from s in db.Students
                              from p in db.Projects
                              from g in db.Groups

                              where getcateId == g.GId && g.PId == p.PId
                              select new { p.Title, g.GId, p.AdvisorName };
                     foreach (var stp in q1)
                     {
                         ViewBag.PTitle = stp.Title;
                         ViewBag.GId = stp.GId;
                         ViewBag.Advisor = stp.AdvisorName;
                     }
            
                     ViewBag.id = getcateId;

                    return View();
                }*/
        public ActionResult StudentView()
        {
            var userName = WebSecurity.CurrentUserName;

            var q2 = from g1 in db.Groups
                     select g1;
            foreach (var gquery in q2)
            {
                Group gg = new Group();
                gg.GId = gquery.GId;
                global.glist.Add(gg);
            }
            var q1 = from s in db.Students
                     from p in db.Projects
                     from g in db.Groups



                     where s.Std_Email == userName && g.GId == s.GId && g.PId == p.PId
                     select new { p.Title, g.GId, p.AdvisorName };
            foreach (var stp in q1)
            {
                ViewBag.PTitle = stp.Title;
                ViewBag.GId = stp.GId;

                ViewBag.Advisor = stp.AdvisorName;
                global.Gid = stp.GId;
            }
            return View();
        }

        [HttpPost]
        public string Upload(FormCollection c, HttpPostedFileBase Attach)
        {
            SubmitArtifact ar = new SubmitArtifact();
            // ar.GId = getcateId;

            ar.GId = global.Gid;
            string comment = c["Comment"];
            ar.Comment_S = comment;
            string artifact = c["ArtifactTypes"];
            ar.ArtifactTitle = artifact;
            //uploading file       
            var serverPath = "";
            string path = "";

            if (Attach != null)
            {
                path = "Data/" + Attach.FileName;
                serverPath = Server.MapPath(@"/Data/" + Attach.FileName);
                Attach.SaveAs(serverPath);
            }
            ar.FilePath = path;

            db.SubmitArtifacts.Add(ar);

            db.SaveChanges();
            return "uploaded";
        }
        

    }
}
